﻿/* jquery plugins */

$.closePopUp = function () {
    $('.overlay').click();
};

$.fn.openPopUp = function (options) {
    var defaults = {
        width: 422,
        cancel: function () { },
        close: function () { },
        confirm: function () { },
        onCreate: function () { },
        popupClass: 'popup',
        additionalCloseButtons: '',
        additionalCancelButtons: '',
        additionalConfirmButtons: '',
        id: ''
    };
    var options = $.extend(defaults, options);
    var popup,
		overlay,
		source = $(this);
    overlay = $('<div class="overlay"></div>').prependTo('body');
    popup = $('<div id="' + options.id + '" class="' + options.popupClass + '"></div>').appendTo('body form');
    popup.append('<div class="content-top"><a class="close" href="javascript:void(0);">Close</a></div><div class="content-middle"><div class="content"></div></div><div class="content-bottom"></div>');

    popup.addClass('popup-' + options.width)
    popup.find('.content-top, .content-bottom').width(options.width);
    popup.find('.content-middle').width(options.width - 9);

    source.children().clone().appendTo(popup.find('.content'));
    source.html('');

    options.onCreate(popup);

    overlay.css('height', $(document).height());
    popup.css('margin-left', ((popup.width() / 2) * -1));
    popup.css('top', ($(document).scrollTop() + Math.round($(window).height() / 2)) - (popup.height() / 2))

    // cancel buttons

    popup.find('.cancel').click(function () {
        options.cancel();
        copyContentToSource();
        popup.remove();
        overlay.remove();
    });

    if (options.additionalCancelButtons != '') {
        popup.find(options.additionalCancelButtons).click(function () {
            options.cancel();
            copyContentToSource();
            popup.remove();
            overlay.remove();
        });
    }

    // confirm buttons

    popup.find('.confirm').click(function () {
        if (options.confirm() === undefined) {
            options.confirm();
            copyContentToSource();
            popup.remove();
            overlay.remove();
        }
    });

    if (options.additionalConfirmButtons != '') {
        popup.find(options.additionalConfirmButtons).click(function () {
            options.confirm();
            copyContentToSource();
            popup.remove();
            overlay.remove();
        });
    }

    // close buttons

    overlay.click(function () {
        options.close();
        copyContentToSource();
        $(this).remove();
        popup.remove();
    });

    popup.find('.close').click(function () {
        options.close();
        copyContentToSource();
        popup.remove();
        overlay.remove();
    });

    if (options.additionalCloseButtons != '') {
        $(options.additionalCloseButtons).click(function () {
            options.close();
            copyContentToSource();
            popup.remove();
            overlay.remove();
        });
    }

    function copyContentToSource() {
        popup.find('.content').children().appendTo(source);
    }

};